package cn.gailvlun.gll.presentation.emotion.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.util.DensityUtil;



public class GllMonthView extends MonthView {

    private Rect srcRect;
    private Rect dstRect;
    
    private Paint futurePaint;
    
    private Paint mCurMonthTextWithIconPaint;
    
    private Paint mCurMonthSelectTextWithIconPaint;

    public GllMonthView(Context context) {
        super(context);
        srcRect = new Rect();
        dstRect = new Rect();
        mCurMonthTextPaint.setFakeBoldText(false);
        mSelectTextPaint.setFakeBoldText(false);

        futurePaint = new Paint(mCurMonthTextPaint);
        
        futurePaint.setColor(getResources().getColor(R.color.greyishBrown));

        mCurMonthTextWithIconPaint = new Paint(mCurMonthTextPaint);
        float textSize = getResources().getDisplayMetrics().density * 8;
        mCurMonthTextWithIconPaint.setTextSize(textSize);
        mCurMonthSelectTextWithIconPaint = new Paint(mSelectTextPaint);
        mCurMonthSelectTextWithIconPaint.setColor(getResources().getColor(R.color.greyishBrown));
        mCurMonthSelectTextWithIconPaint.setTextSize(textSize);
    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        return false;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {

    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        if (!calendar.isCurrentMonth()) return;
        Drawable emotionDrawable = null;
        if (drawableProvider != null) {
            emotionDrawable = drawableProvider.getDrawable(calendar);
        }
        String day = String.valueOf(calendar.getDay());
        float textX;
        float textY;
        float density = getResources().getDisplayMetrics().density;


        int width = DensityUtil.dp2px(getContext(),33f);
        int height = DensityUtil.dp2px(getContext(),33f);
        boolean hasIcon = false;
        if (emotionDrawable != null && emotionDrawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) emotionDrawable).getBitmap();
            srcRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
            int l = x + (mItemWidth - width) / 2;
            int t = y + (mItemHeight - height) / 2;
            int r = l + width;
            int b = t + height;
            dstRect.set(l, t, r, b);
            canvas.drawBitmap(bitmap, srcRect, dstRect, mCurMonthTextPaint);
            textX = r + density * 4;
            textY = mTextBaseLine + y + height / 4 - density * 2;
            hasIcon = true;
        } else {
            if (emotionDrawable != null) emotionDrawable.draw(canvas);
            textX = x + mItemWidth / 2;
            textY = mTextBaseLine + y ;
        }
        Paint paint;
        if (isFuture2(calendar)) {
            paint = futurePaint;
        } else {
            if (hasIcon) {
                paint = isSelected ? mCurMonthSelectTextWithIconPaint : mCurMonthTextWithIconPaint;
            } else {
                paint = isSelected ? mSelectTextPaint : mCurMonthTextPaint;
            }

            if (isSelected) {
                float radius = density * 3;
                float cx = x + mItemWidth / 2;
                float cy = y + (mItemHeight + height) / 2 + density * 6;
                canvas.drawCircle(cx, cy, radius, mSelectTextPaint);
            }
        }
        canvas.drawText(day, textX, textY, paint);

    }

    protected boolean isFuture(Calendar calendar) {
        String time = calendar.getYear() + "/" + calendar.getMonth() + "/" + calendar.getDay();
        int comp = LocalDate.now().compareTo(LocalDate.parse(time, DateTimeFormat.forPattern("yyyy/MM/dd")));
        return comp == -1;
    }

    protected boolean isFuture2(Calendar calendar) {
        String time = calendar.getYear() + "/" + calendar.getMonth();
        int comp = LocalDate.now().minusMonths(1).monthOfYear().compareTo(LocalDate.parse(time, DateTimeFormat.forPattern("yyyy/MM")));
        return comp == -1;
    }

}
